<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Mail/Mailer.php
 * 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Mail
 */
class GEM_Mail_Mailer
{
	/**
	 * 
	 * Options array
	 * options => array(
	 * 		'from' => array( 
	 * 				'email' => 'email@site.net,
	 * 				'name'	=> 'Name'
	 * 		),
	 * 		'body' 		=> 'Body...',
	 * 		'to'   		=> array($email,...,$emailN),
	 * 		'subject' 	=> 'Subject...'	
	 * 	)
	 * 
	 * @todo Zend_Mail does not verify if the email was truly sent successfully. 
	 * Good luck to any developers willing to solve this problem.
	 * 
	 * @param array $options
	 * @return boolean
	 */
	public static function send(array $options)
	{
		//@todo Should be examining options array for correct structure. 
		$config = Zend_Registry::get('config');
		$tr = new Zend_Mail_Transport_Smtp(
		$config->email->incoming,
			array(
				'auth'		=> 'plain',
				'port' 		=> $config->email->port,
				'username' 	=> $config->email->username,
				'password' 	=> $config->email->password
			));
		$mail = new Zend_Mail();
		$mail->setFrom($options['from']['email'],$options['from']['name'])
			 ->setBodyHtml($options['body'])
			 ->setSubject($options['subject']);
		foreach($options['to'] as $to){
			$mail->addTo($to);
		}
		try{
			$mail->send($tr);
		} catch(Zend_Exception $e) {
			return false;
		}
		return true;
	}
}